package com.fhz.main.ui.home

import android.annotation.SuppressLint
import android.os.Bundle
import android.view.Gravity
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.TextView
import androidx.fragment.app.Fragment
import androidx.lifecycle.ViewModelProvider
import androidx.recyclerview.widget.StaggeredGridLayoutManager
import com.alibaba.android.arouter.launcher.ARouter
import com.fhz.common.entity.MeiNv
import com.fhz.common.router.Path
import com.fhz.loginmvvm.callback.ApiSuccessResponse
import com.fhz.main.databinding.FragmentHomeBinding
import dagger.hilt.android.AndroidEntryPoint
import dagger.hilt.android.HiltAndroidApp

@AndroidEntryPoint
class HomeFragment : Fragment() {

    private var _binding: FragmentHomeBinding? = null

    // This property is only valid between onCreateView and
    // onDestroyView.
    private val binding get() = _binding!!

    override fun onCreateView(
        inflater: LayoutInflater,
        container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View {
        val homeViewModel = ViewModelProvider(this)[HomeViewModel::class.java]
        _binding = FragmentHomeBinding.inflate(inflater, container, false)
        val root: View = binding.root

        homeViewModel.text.observe(viewLifecycleOwner) {
        }

        //适配器
        val meiNvList = ArrayList<MeiNv>()
        binding.rvHome.layoutManager = StaggeredGridLayoutManager(2,StaggeredGridLayoutManager.VERTICAL)
        homeViewModel.getMeiNv("beauty",1,20).observe(viewLifecycleOwner){
//            println(it)
            val apiSuccessResponse = it as ApiSuccessResponse
            val homeRVAdapter = HomeRVAdapter(context,apiSuccessResponse.body.result.list)
            homeRVAdapter.addAll(apiSuccessResponse.body.result.list)
            homeRVAdapter.notifyDataSetChanged()
            binding.rvHome.adapter = homeRVAdapter

        }



        binding.imHomeSearch.setOnClickListener{
            val text = binding.etHomeInput.text
            //发送网络请求
        }

        return root
    }

    override fun onDestroyView() {
        super.onDestroyView()
        _binding = null
    }
}